import getpath
import os
import openpyxl
from openpyxl.drawing.image import Image
from PyQt5 import QtWidgets,QtGui
import io
# from typing import Tuple
from PIL import Image as PILImage
import ImageQt
def output_curve(ws,hang):#outputsheet,pdf filename,0-n page
    ts=get_images()
    outputImages(ts,ws,hang)
def output(imgs):
    wb = openpyxl.Workbook()
    ws = wb.active
    outputImages(imgs,ws,1)
    wb.save("imags.xlsx")
def outputImages(ts,ws,hang):#outputsheet,pdf filename,0-n page
    ss=QtWidgets.qApp.screens()
    if len(ss)<1:
        rowh=13.5/72.0*96
    else:
        rowh=13.5/72.0*ss[0].physicalDotsPerInch() #bang to pixel
    for i in range(len(ts)):
        img=ts[i]
        pimg = ImageQt.fromqimage(img)
        img = Image(pimg)
        # img.width=300#not too big
        # img.height=200
        n=int((img.height)/rowh)
        at_row=i*n+hang
        ws.column_dimensions['A'].width = 30.0
        ws.add_image(img, "A"+str(at_row))  # 图片 插入 A1 的位置上
def get_images():
    crntDir = os.path.join(getpath.getpath(),"images")
    # create table
    list_data = []
    philes = os.listdir(crntDir)
    for phile in philes:
        if phile.endswith(".png") or phile.endswith("jpg"):
            fn=os.path.join(crntDir, phile)
            pixmap=QtGui.QPixmap(fn)
            list_data.append(pixmap)
    return list_data
def test():
    wb = openpyxl.Workbook()
    ws = wb.active
    output_curve(ws,1)
    wb.save("imags.xlsx")
if __name__=="__main__":
    a = QtWidgets.QApplication([])
    test()
    a.quit()
